<template>
  <div class="check-company">
    <div class="select-company">
      <!-- <Select
        v-model="serviceId"
        style="width: 200px"
        placeholder="请选择产业"
        @on-change="serviceChange"
      >
        <Option
          v-for="item in companyService"
          :value="item.dictId"
          :key="item.dictId"
          >{{ item.name }}</Option
        >
      </Select> -->
      <Select
        v-model="parentCompanyId"
        placeholder="请输入企业搜索"
        filterable
        clearable
        :remote-method="remoteMethod"
        :loading="loading"
        @on-change="companyChange"        
      >
        <Option
          v-for="item in companyList"
          :value="item.companyId"
          :key="item.companyId"
          >{{ item.companyName }}</Option
        >
      </Select>
    </div>
  </div>
</template>

<script>
import {
  GetDict, // 获取证件类型
} from "@/api/common.js";

import { 
  getCompanySelectByCompanyName, // 根据名称获取组织机构
 } from "@/api/system.js";

export default {
  data() {
    return {
      companyService: [],
      companyList: [],
      serviceId: "",
      parentCompanyId: "",
      loading: false,
    };
  },
  methods: {
    async getDictData(key, name) {
      let data = await GetDict({
        parentId: key,
      });
      this[name] = data;
    },
    // 获取上级组织
    async getCompanySelectByCompanyNameData(companyName) {
      let params = {
        companyName,
      };
      let data = await getCompanySelectByCompanyName(params);
      this.companyList = data;
    },
    async remoteMethod(query) {
      let isName = ['有限公司','公司']
      if (query !== "" && !isName.includes(query)) {
        this.loading = true;
        let params = {
          companyName: query,
        };
        let data = await getCompanySelectByCompanyName(params);
        this.loading = false;
        this.companyList = data;
        // setTimeout(() => {
        //   this.loading = false;
        //   const list = this.list.map((item) => {
        //     return {
        //       value: item,
        //       label: item,
        //     };
        //   });
        //   this.options1 = list.filter(
        //     (item) => item.label.toLowerCase().indexOf(query.toLowerCase()) > -1
        //   );
        // }, 200);
      } else {
        this.companyList = [];
      }
    },
    serviceChange(item) {
      this.getCompanySelectByCompanyNameData(item);
    },
    companyChange(item) {
      let itemdata = this.companyList.find((o) => o.companyId == item);
      console.log(item, itemdata, "itemdata");
      // this.$emit("updade:parentCompanyId", item);
      this.$emit("parentCompanyObj", itemdata);
      this.$emit("update:parentCompanyId", item);
    },
  },
  mounted() {
    this.getDictData("COMPANY_SERVICE", "companyService"); // 获取公司服务能力
  },
};
</script>

<style lang="less" scoped>
.select-company {
  display: flex;
  border: 1px solid #c4c4c4;
  border-radius: 4px;
  ::v-deep(.ivu-select-selection) {
    border: none;
  }
}
</style>
